const KeyPrefix = 'os-', // 键前缀
  // 缓存的键
  localStorageKeys = ['id','app','file'], // 保存在localStorage中的键
  localkeys = [...localStorageKeys],
  sessionkeys = ['token','username'],

  // 操作
  operation =
{
  get ()
  {
    const key = `${KeyPrefix}${this.key}`;

    return window.localStorage.getItem(key);
  },
  set (value)
  {
    const key = `${KeyPrefix}${this.key}`;
    if(this.key==='app') {
      return window.localStorage.setItem(key, JSON.stringify(value));
    }
    window.localStorage.setItem(key, value);
  },
  remove ()
  {
    const key = `${KeyPrefix}${this.key}`;

    window.localStorage.removeItem(key);
  }
},
  // 操作
  operation1 =
{
  get ()
  {
    const key = `${KeyPrefix}${this.key}`;

    return window.sessionStorage.getItem(key);
  },
  set (value)
  {
    const key = `${KeyPrefix}${this.key}`;

    window.sessionStorage.setItem(key, value);
  },
  remove ()
  {
    const key = `${KeyPrefix}${this.key}`;

    window.sessionStorage.removeItem(key);
  }
},

CacheService = {};

localkeys.forEach((key) =>
{
  CacheService[key] =
  {
    key,
    ...operation
  };
});
sessionkeys.forEach((key) =>
{
  CacheService[key] =
  {
    key,
    ...operation1
  };
});

export default CacheService;
